#include "GameoverScene.h"
#include "HelloWorldScene.h"
#include "Menu.h"
using namespace cocos2d;

CCScene* Gameover::scene()
{
	CCScene *scene = NULL;
	do
	{
		scene = CCScene::create();
		CC_BREAK_IF(!scene);
		Gameover *layer = Gameover::create();
		CC_BREAK_IF(!layer);
		scene->addChild(layer);
	} while (0);
	return scene;
}

bool Gameover::init()
{
	bool bRet = false;
	do
	{
		CC_BREAK_IF(!CCLayer::init());

		CocosDenshion::SimpleAudioEngine::sharedEngine()->stopBackgroundMusic();


		CCSize size = CCDirector::sharedDirector()->getWinSize();

		CCSprite * bg = CCSprite::create("background.jpg");
		CC_BREAK_IF(!bg);
		bg->setPosition(ccp(size.width/2,size.height/2));
		this->addChild(bg,0);

		CCLabelTTF *pOver = CCLabelTTF::create("Game Over","Arial",40);
		CC_BREAK_IF(!pOver);
		pOver->setPosition(ccp(size.width/2,size.height/2 + 100));
		this->addChild(pOver,2);

		CCMenuItemImage *pStartItem = CCMenuItemImage::create(
            "reset.png",
            "reset.png",
            this,
			menu_selector(Gameover::resetGame));	
		CC_BREAK_IF(!pStartItem);
		pStartItem->setPosition(ccp(size.width/2,size.height/2+50));

		CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
            "Exit.png",
            "Exit.png",
            this,
			menu_selector(Gameover::menuCloseCallBack));	
		CC_BREAK_IF(!pCloseItem);
		pCloseItem->setPosition(ccp(size.width/2,size.height/2-50));

		CCMenu *menu = CCMenu::create(pStartItem,pCloseItem,NULL);
		menu->setPosition(CCPointZero);
		CC_BREAK_IF(!menu);
		this->addChild(menu,1);

		CCActionInterval *actTo = CCBlink::create(10,10);
		pOver->runAction(actTo);

		bRet = true;
	} while (0);
	return bRet;
}

void Gameover::resetGame(CCObject* p)
{
	CCScene *s = HelloWorld::scene();
	CCDirector::sharedDirector()->replaceScene(CCTransitionSlideInL::create(1,s));
}

void Gameover::menuCloseCallBack(CCObject* pSender)
{
	CCDirector::sharedDirector()->end();
}
